package com.customer.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.customer.dao.ClientMapper;
import com.customer.service.ClientService;
import com.github.pagehelper.PageHelper;
import com.nameless.entity.Client;
import com.nameless.util.JwtUtil;
import com.nameless.util.WebUtils;
import com.nameless.vo.ClientVo;
import com.nameless.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author WHZ
 * @date 2023年02月27日 16:40
 */
@RestController
@RequestMapping("/client")
public class ClientController {
    @Autowired
    private ClientService clientService;

    @Autowired
    private ClientMapper clientMapper;



    /**
     * 分页查询并模糊查询
     * @param currentPage
     * @param pageSize
     * @param clientVo
     * @return
     */
    @PostMapping("/list")
    public Result<List<Client>> list(Integer currentPage, Integer pageSize, @RequestBody ClientVo clientVo){
        PageHelper.startPage(currentPage,pageSize);
        return clientService.findAll(currentPage, pageSize, clientVo);

    }

    /**
     * 根据id删除
     */

    @DeleteMapping("/delete")
    public Result delete(Integer id) {
        int i = clientMapper.deleteById(id);
        return new Result(200,"删除成功",i);
    }

    /**
     * 添加
     * @param client
     * @return
     */
    @PostMapping("insert")
    public Result insert(@RequestBody Client client){
        return clientService.add(client);
    }

    /**
     * 根据Id修改
     * @param client
     * @return
     */
    @PutMapping("updateById")
    public Result updateById(@RequestBody Client client){
        int update = clientMapper.updateById(client);
        return new Result(200,"修改成功",update);
    }

    @PutMapping("updateBy")
    public Result updateBy(@RequestBody Client client){
        Client allUserName = clientMapper.getAllUserName(client.getUsername());
        if (allUserName==null) {
            int update = clientMapper.updateById(client);
            return new Result(200, "修改成功", update);
        }else {
            return new Result(500, "该用户名已被占用");
        }
    }

    @RequestMapping("/updatesfz")
    public Result update(@RequestBody Client client){
        HttpServletRequest request = WebUtils.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        String username = tokenChaim.get("clientname").toString();
        LambdaQueryWrapper<Client> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Client> eq = queryWrapper.eq(Client::getUsername, username);
        Client client1 = clientMapper.selectOne(eq);
        Integer id = client1.getId();
        client.setId(id);
        int i = clientMapper.updateById(client);
        return new Result(200,"修改成功",i);
    }

    @RequestMapping("/getClient/{clientName}")
    public Client getClient(@PathVariable String clientName){
       return clientService.getClient(clientName);
    }

}
